package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.FoodDao;
import pojo.Categories;
import pojo.Food;
import pojo.Merchant;

@Repository
public class FoodDaoImpl implements FoodDao {

	@PersistenceContext(name="un")
	private EntityManager manager; 
	
	@Override
	public List<Food> findFoodByCategoryPage(String mId, String typeId, int current, int pageSize) {
		return null;
	}
	
	@Override
	public List<Food> findByCategory(String mId, String typeId) {
		String hq = "select f from Food f where f.merchant.id = ? and f.categories.id = ?";
		List<Food> foodList = (List<Food>) manager.createQuery(hq)
				.setParameter(1, mId)
				.setParameter(2, typeId)
				.getResultList();
		return foodList;
	}

	@Override
	public List<Food> findByMerchantPage(String mId, int pageNo, int pageSize) {
		String hq = "select f from Food f where f.merchant.id = ?";
		List<Food> foodList = (List<Food>) manager.createQuery(hq)
				.setParameter(1, mId)
				.setFirstResult((pageNo-1)*pageSize)
				.setMaxResults(pageSize)
				.getResultList();
		
		return foodList;
	}

	@Override
	public List<Food> findByMerchant(String mId) {
		String hq = "select f from Food f where f.merchant.id = ?";
		List<Food> foodList = (List<Food>) manager.createQuery(hq)
				.setParameter(1, mId)
				.getResultList();
		return foodList;
	}
	
	@Override
	public int getCountByCategory(String mId, String typeId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCountByMerchant(String mId) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	

	@Override
	public Food delete(String id) {
		String hqCategories = "select f.categories from Food f where f.id = ?";
		String hqMerchant = "select f.merchant from Food f where f.id = ?";
		
		Categories categories = (Categories)manager.createQuery(hqCategories)
		.setParameter(1, id)
		.getSingleResult();
		
		Merchant merchant = (Merchant)manager.createQuery(hqMerchant)
				.setParameter(1, id)
				.getSingleResult();
		
		System.out.println(merchant);
		Food food = load(id);
		categories.getFoodList().remove(food);
		merchant.getFoodList().remove(food);
		manager.remove(food);
		
		return food;
	}

	@Override
	public Food load(String id) {
		return manager.find(Food.class, id);
	}

//	@Override
//	public Food update(Food food) {
//		String hqCategories = "select f.categories from Food f where f.id = ?";
//		String hqMerchant = "select f.merchant from Food f where f.id = ?";
//		String id = food.getId();
//		Food food1 = manager.find(Food.class,id);
//		Categories categories = (Categories)manager.createQuery(hqCategories)
//				.setParameter(1, id)
//				.getSingleResult();
//				
//		Merchant merchant = (Merchant)manager.createQuery(hqMerchant)
//				.setParameter(1, id)
//				.getSingleResult();
//		
//		for(Food f:categories.getFoodList()){
//			if(f.getId().equals(id)){
//				f = food;
//			}
//		}
//		
//		for(Food f:merchant.getFoodList()){
//			if(f.getId().equals(id)){
//				f = food;
//			}
//		}
//		food1 = food;
//		return food1;
//	}
	@Override
	public Food update(Food food) {
		
		String jpql = "update Food f set f.name=?,f.price=?,f.image=?,f.categories=? where f.id=?";
		manager.createQuery(jpql)
		.setParameter(1, food.getName())
		.setParameter(2, food.getPrice())
		.setParameter(3, food.getImage())
		.setParameter(4, food.getCategories())
		.setParameter(5, food.getId())
		.executeUpdate();
		return food;
	}
	
	@Override
	public void add(Categories categories, Merchant merchant) {
		manager.persist(categories);
		manager.persist(merchant);
	}

}
